package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;
	@GetMapping("api/user/userInfo/updateVipExpireStatus")
	Result updateVipExpireStatus(){
		userInfoService.updateVipExpireStatus();
		return Result.ok();
	}


	/**
	 * 处理用户购买记录
	 * @param userPaidRecordVo
	 * @return
	 */
	@PostMapping("/savePaidRecord")
	Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){
		userInfoService.savePaidRecord(userPaidRecordVo);
		return Result.ok();

	}

	/**
	 * 获取专辑已支付的声音Id集合列表
	 * @param albumId
	 * @return
	 */
	@TsLogin
	@GetMapping("findUserPaidTrackList/{albumId}")
	Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId){
		Long userId = AuthContextHolder.getUserId();
		List<Long> list= userInfoService.findUserPaidTrackList(userId,albumId);
		return Result.ok(list);
	}

	@GetMapping("api/user/userInfo/userIsPaidTrack/{albumId}")
	Result<Map<Long, Integer>> getUserPaidTrackIdList(@PathVariable Long albumId, @RequestBody List<Long> paidTrackIdList){
		Long userId = AuthContextHolder.getUserId();
		Map<Long, Integer> map=userInfoService.getUserPaidTrackIdList(albumId,paidTrackIdList,userId);
		return Result.ok(map);
	}


	@Operation(summary = "根据用户id获取用户信息")
	@GetMapping("getUserInfoVo/{userId}")
	public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId)
	{

		UserInfoVo userInfoVo= userInfoService.getUserInfo(userId);
		return Result.ok(userInfoVo);
	}
	@GetMapping("api/user/userInfo/isPaidAlbum/{albumId}")
	Result<Boolean> isPaidAlbum(@PathVariable("albumId") Long albumId){
		Long userId = AuthContextHolder.getUserId();
		Boolean flag=userInfoService.isPaidAlbum(userId,albumId);
		return Result.ok(flag);
	}
}

